﻿<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
</head>
<body>
<script type="text/javascript">

    // 2.0
            new treeforms.parser({
                forms: {
                    name: "营业时间",
                    selector: ".hours>.body>ul>li",
                    path: "/ResponseData/Children",
                    children: [
                        {
                            name: "指定或未指定的处理",
                            selector: ".hours_type",
                            join: { selector: "tree_name", path: "Name/text()" },
                            trigger: [
                                { event: "click", action: "commit" },
                                { event: "selected", traversing: [
                                        { selector: ":eq(0)", action: "unenabled", target: "每周" },
                                        { selector: ":eq(1)", action: "enabled, bind", target: "每周"}] }
                            ],
                            bind: { form: "checked", to: "Selected" },
                            action: {
                                selected: { type: "option" },
                                "default": { selector: ":eq(0)", value: true }
                            }
                        },
                        {
                            name: "每周",
                            selector: ".hours_table tr",
                            path: "/Children",
                            join: { attr: "tree_name", path: "Name/text()" },
                            children: [
                                {
                                    name: "时间范围开关",
                                    selector: ".closed",
                                    path: "/Children[Name/text() = '不营业']",
                                    trigger: [
                                        { event: "click", action: "commit" },
                                        { event: "selected", target: { action: "unenabled", name: "选择时间范围" }, "switch": true },
                                        { event: "unselected", backtracking: { action: "selected"} }
                                    ],
                                    bind: { form: "checked", to: "Selected" },
                                    action: {
                                        "default": { value: true }
                                    }
                                },
                                {
                                    name: "选择时间范围",
                                    selector: ".am, .pm",
                                    path: "/Children[Name/text() != '不营业']",
                                    join: { attr: "tree_name", path: "Name/text()" },
                                    trigger: [
                                        { event: "click", action: "commit" },
                                        { event: "enabled", action: "selected", "switch": true }
                                    ],
                                    bind: { form: "value", to: "Memo" },
                                    action: {
                                        "default": [{ selector: ".am", value: "8:00" },
                                                    { selector: ".pm", value: "21:00"}]
                                    }
                                }
                            ]
                        },
                    ]
                }, 
                tree: function(callback) {
                    $.get("merchant.axd", {
                        CId: "748D7F3C-F21F-DE11-9504-000000004188", Action: "GCGC", RGCId: "90C70C7E-2A27-DE11-9504-000000004188", LangId: 4, format: "xml"
                    }, function(data) {
                        callback(data);
                    });
                }, 
                commit: function(changes) {
                    $.post("merchant.axd", {
                        CId: "748D7F3C-F21F-DE11-9504-000000004188", Action: "UCGC", RGCId: "90C70C7E-2A27-DE11-9504-000000004188", LangId: 4, STData: changes
                    });
                }
            });



            TreeForm.Mapping.addMapping(
                {
                    name: "支付方式",
                    selector: ".booking_type_list",
                    path: "/ResponseData",
                    children: [
                        {
                            name: "具体各种的方式",
                            selector: "li",
                            path: "/Children",
                            join: { attr: "tree_name", path: "Name/text()" },
                            bind: { form: true, to: "Selected" },
                            children: [
                                {
                                    name: "预订方式-是否选择",
                                    selector: "input:checkbox",
                                    trigger: [
                                        { event: "click", action: "commit" },
                                        { event: "selected", target: { action: "enabled", name: "预订方式-值" }, "switch": true }
                                    ],
                                    bind: { form: "checked", to: "Selected" }
                                },
                                {
                                    name: "预订方式-值",
                                    selector: "input:text",
                                    trigger: [
                                        { event: "click", action: "commit"}],
                                    bind: { form: "value", to: "Memo" },
                                    action: {
                                        enabled: { style: "display" },
                                        unenabled: { style: "display" }
                                    }
                                }
                            ]
                        }
                    ]
                }, function(callback) { // get tree
                    $.get("merchant.axd", {
                        CId: "748D7F3C-F21F-DE11-9504-000000004188", Action: "GCGC", RGCId: "8BA6AD54-5B1E-DE11-9504-000000004188", LangId: 4, format: "xml"
                    }, function(data) {
                        callback(data);
                    });
                }, function(changes) { // update tree
                    $.post("merchant.axd", {
                        CId: "748D7F3C-F21F-DE11-9504-000000004188", Action: "UCGC", RGCId: "8BA6AD54-5B1E-DE11-9504-000000004188", LangId: 4, STData: changes
                    });
                });

            TreeForm.Mapping.addMapping(
                {
                    name: "预订方式",
                    selector: ".booking_type_list",
                    path: "/ResponseData",
                    children: [
                        {
                            name: "是否启用预订方式",
                            selector: "#chk_online_payament",
                            trigger: { event: "click", action: "commit" },
                            bind: { form: "checked", to: "Selected" }
                        },
                        {
                            name: "预订方式列表",
                            selector: "li",
                            path: "/Children",
                            children: [
                                {
                                    name: "前台付款或在线付款",
                                    selector: ">label>input:checkbox",
                                    trigger: { event: "click", action: "commit" },
                                    bind: { form: "checked", to: "Selected" }
                                },
                                {
                                    name: "前台付款",
                                    selector: ".payment_type",
                                    join: { attr: "tree_name", path: "Name/text()", value: "前台付款" },
                                    children: [
                                        {
                                            name: "前台付款的种类-信用卡",
                                            selector: ">input:radio",
                                            path: "/Children[Name/text()='信用卡']",
                                            trigger: [
                                                { event: "click", action: "commit" },
                                                { event: "selected", traversing: { action: "enabled" }, "switch": true }
                                            ],
                                            bind: { form: "checked", to: "Selected" },
                                            children: [
                                                {
                                                    name: "前台付款的种类-信用卡-卡种",
                                                    selector: "input:checkbox",
                                                    path: "/Children",
                                                    trigger: [
                                                        { event: "click", action: "commit" },
                                                        { event: "enabled", action: "selected" }
                                                    ],
                                                    bind: { form: "checked", to: "Selected" },
                                                    action: {
                                                        enabled: { style: "display" }, unenabled: { style: "display" }
                                                    }
                                                }
                                            ]
                                        },
                                        {
                                            name: "前台付款的种类-其他",
                                            selector: ">input:radio",
                                            path: "/Children[Name/text()!='信用卡']",
                                            trigger: { event: "click", action: "commit" },
                                            bind: { form: "checked", to: "Selected" }
                                        }
                                    ]
                                },
                                {
                                    name: "在线付款",
                                    selector: ".payment_type",
                                    join: { attr: "tree_name", path: "Name/text()", value: "在线付款" },
                                    children: [
                                        {
                                            name: "在线付款方式",
                                            selector: ">li>ul>li",
                                            path: "/Children",
                                            children: [
                                                {
                                                    name: "在线付款方式-旅银",
                                                    selector: "input:radio:eq(0)",
                                                    path: "/Children[Name/text()!='信用卡']",
                                                    trigger: { event: "click", action: "commit" },
                                                    bind: { form: "checked", to: "Selected" }
                                                },
                                                {
                                                    name: "在线付款方式-百分比",
                                                    selector: ">li>ul>li",
                                                    path: "/Children[Name/text()!='信用卡']/Children",
                                                    join: { attr: "tree_name", path: "Name/text()" },
                                                    children: [
                                                        {
                                                            name: "在线付款方式-百分比-是否选择",
                                                            selector: "input:checkbox",
                                                            trigger: [
                                                                { event: "click", action: "commit" },
                                                                { event: "selected", target: { action: "enabled", name: "在线付款方式-百分比-值" }, "switch": true }
                                                            ],
                                                            bind: { form: "checked", to: "Selected" }
                                                        },
                                                        {
                                                            name: "在线付款方式-百分比-值",
                                                            selector: "input:text",
                                                            trigger: { event: "click", action: "commit" },
                                                            bind: { form: "value", to: "Memo" },
                                                            action: {
                                                                enabled: { style: "display" },
                                                                unenabled: { style: "display" }
                                                            }
                                                        }
                                                    ]
                                                },
                                                {
                                                    name: "在线付款方式-信用卡",
                                                    selector: "input:radio:eq(1)",
                                                    path: "[Name/text()='信用卡']",
                                                    trigger: [
                                                        { event: "click", action: "commit" },
                                                        { event: "enabled", action: "selected" }
                                                    ],
                                                    bind: { form: "checked", to: "Selected" },
                                                    action: {
                                                        enabled: { style: "display" }, unenabled: { style: "display" }
                                                    }
                                                }
                                            ]
                                        }
                                    ]
                                }
                            ]
                        }
                    ]
                }, function(callback) { // get tree
                    $.get("merchant.axd", {
                        CId: "748D7F3C-F21F-DE11-9504-000000004188", Action: "GCGC", RGCId: "8BA6AD54-5B1E-DE11-9504-000000004188", LangId: 4, format: "xml"
                    }, function(data) {
                        callback(data);
                    });
                }, function(changes) { // update tree
                    $.post("merchant.axd", {
                        CId: "748D7F3C-F21F-DE11-9504-000000004188", Action: "UCGC", RGCId: "8BA6AD54-5B1E-DE11-9504-000000004188", LangId: 4, STData: changes
                    });
                });

    // 1.0
                TreeForm.Mapping.addMapping(
            {
                name: "营业时间",
                container: ".hours",
                data: "/ResponseData",
                children: [
            {
                name: "是否指定",
                container: ".body>ul>li",
                data: "/Children",
                handle: [
            { when: "Name[text()='不指定营业时间']", selector: ".hours_type[value='0']",
                on: "click", datafield: "selected", attr: "checked", "default": true, datatype: "option",
                event: { changed: { when: true, action: "switchEnable", param: false} }
            },
            { when: "Name[text()='指定营业时间']", selector: ".hours_type[value='1']",
                on: "click", datafield: "selected", attr: "checked", datatype: "option",
                event: { changed: { when: true, action: "switchEnable", param: true} }
            }
            ],
                children: [
            {
                name: "每周",
                container: ".hours_table tr",
                data: "/Children",
                join: { attr: "tree_name", datafield: "Name" },
                children: [
            {
                name: "时间范围开关",
                data: "/Children",
                handle: { when: "Name[text() = '不营业']", selector: ".closed",
                    on: "click", datafield: "selected", attr: "checked", "default": false, datatype: "option",
                    event: [{ changed: { action: "switchVisible", param: function(val) { return !val; } } },
            { changed: { action: "enableTree", param: function(val) { return !val; } }}]
                },
                children: [
            {
                name: "选择时间范围",
                data: "[Name/text() != '不营业']",
                handle:
                [
            { when: "Name[text()='开始时间']", selector: ".am", on: "change", datafield: "memo", attr: "value", "default": "8:00", datatype: "value" },
            { when: "Name[text()='结束时间']", selector: ".pm", on: "change", datafield: "memo", attr: "value", "default": "21:00", datatype: "value" }
            ]
            }
            ]
            }
            ]
            },
            ]
            }
            ]
            }, function(callback) { // get tree
                $.get("merchant.axd", {
                    CId: "748D7F3C-F21F-DE11-9504-000000004188", Action: "GCGC", RGCId: "90C70C7E-2A27-DE11-9504-000000004188", LangId: 4, format: "xml"
                }, function(data) {
                    callback(data);
                });
            }, function(changes) { // update tree
                $.post("merchant.axd", {
                    CId: "748D7F3C-F21F-DE11-9504-000000004188", Action: "UCGC", RGCId: "90C70C7E-2A27-DE11-9504-000000004188", LangId: 4, STData: changes
                });
            }
            );

    // TreeForm
    var TreeForm = function() { this.initialize.apply(this, arguments); };
    TreeForm.prototype = {
        initialize: function(args) {
            this.container = args.container;
            this.datasource = args.datasource || {};
            //this.bindMappings = args.bindMappings || {};
            this.updated = args.updated || {};
            this.actions = args.actions || {};
            this.events = args.events || {
                onInit: function() { },
                onDataBind: function(tree) { },
                onDataNodeBind: function(path, node) { },
                onUpdated: function() { }
            };

        },
        update: function() {
            var cloneDoc = $(this.datasource).clone().get(0);
            jQuery.each(zXPath.selectNodes(cloneDoc, "//Children[Selected/text()='false']"), function() {
                this.parentNode.removeChild(this);
            });
            this.actions.update(
                (new XMLSerializer()).serializeToString(cloneDoc), this.events.onUpdated);
            debugger;
        }
    };

        // TreeFormManager
    window.TreeFormManager = {
        items: [],
        addForm: function(treeForm) {
            TreeFormManager.items.push(treeForm);
            treeForm.actions.initialize(treeForm.container);
            treeForm.events.onInit();
            TreeFormManager.bindForm(treeForm);
        },
        bindForm: function(treeForm) {
            treeForm.actions.dataBind(function(data) {
                // trigger init event
                treeForm.datasource = data;
                TreeFormManager.recursiveTree(data, function(path, node) {
                    // trigger node bind event
                    treeForm.events.onDataNodeBind.call(treeForm, path, node);
                });

                // trigger databind event
                treeForm.events.onDataBind.call(treeForm, data);
            });
        },
        recursiveTree: function(tree, fn) {
            (function recus(path, current) {
                fn(path + TreeFormManager.getTreeNode(current, "Name"), current);
                jQuery.each(zXPath.selectNodes(current, "Children"), function() {
                    recus(path + TreeFormManager.getTreeNode(current, "Name") + "/", this);
                });
            })("/", tree);
        },
        backtrackingTree: function(node, fn) {
            (function recus(path, current) {
                fn(TreeFormManager.getTreeNode(current, "Name") + path, current);
                if (current.parentNode != null && current.tagName != "ResponseData")
                    recus("/" + TreeFormManager.getTreeNode(current, "Name") + path, current.parentNode);
            })("/", node);
        },
        changeTreeNode: function(tree, path, value, memo) {
            var node = zXPath.selectSingleNode(tree, path);
            $(zXPath.selectSingleNode(node, "Selected")).text(value);
            //debugger;
            if (value)
                TreeFormManager.backtrackingTree(node, function(path, current) {
                    $(zXPath.selectSingleNode(current, "Selected")).text(value);
                    consoleLog(node);
                });

            if (memo)
                $(zXPath.selectSingleNode(node, "Memo")).text(memo);
        },
        getTreeNode: function(node, path) {
            //var cap = zXPath.selectSingleNode(node, path);
            //return (cap) ? cap.text : null;
            return zXPath.selectSingleNode(node, path).text;
        },
        update: function() {
            jQuery.each(TreeFormManager.items, function() { this.update(); });
        }
    };

        // Hours
    TreeFormManager.addForm(new TreeForm({
        container: $(".hours").get(0),
        datasource: null,
        updated: {},
        actions: {
            initialize: function(conts) {
                $(".hours_table select", conts).each(function() {
                    this.disabled = "disabled";
                    var defaultAM = 8, defaultPM = 9;
                    for (var hour = 0; hour < 12; hour++) {
                        $(this).append("<option value='" + hour + ":00'" +
                            (this.id.indexOf("begin") != -1 && hour == defaultAM ? " selected" : "") + ">" +
                            (hour < 10 ? "0" : "") + hour + ":00 上午</option>");
                        $(this).append("<option value='" + hour + ":30'>" +
                            (hour < 10 ? "0" : "") + hour + ":30 上午</option>");
                    }
                    for (var hour = 0; hour < 12; hour++) {
                        $(this).append("<option value='" + (12 + hour) + ":00'" +
                            (this.id.indexOf("end") != -1 && hour == defaultPM ? " selected" : "") + ">" +
                            (hour < 10 ? "0" : "") + hour + ":00 下午</option>");
                        $(this).append("<option value='" + (12 + hour) + ":30'>" +
                            (hour < 10 ? "0" : "") + hour + ":30 下午</option>");
                    }
                });
            },
            dataBind: function(callback) {
                var thisObj = this;
                $.get("merchant.axd?callback=?", {
                    format: "xml",
                    CId: "748D7F3C-F21F-DE11-9504-000000004188",
                    Action: "GCGC",
                    RGCId: "90C70C7E-2A27-DE11-9504-000000004188",
                    LangId: 4
                }, function(data) {
                    callback.call(thisObj, zXPath.selectSingleNode(data, "//ResponseData"));
                });
            },
            update: function(changes, callback) {
                $.post("merchant.axd", {
                    CId: "748D7F3C-F21F-DE11-9504-000000004188",
                    Action: "UCGC",
                    RGCId: "90C70C7E-2A27-DE11-9504-000000004188",
                    LangId: 4,
                    STData: changes
                }, callback);
            }
        },
        events: {
            onInit: function() { },
            onDataBind: function(tree) {

                var thisObj = this;

                function bindHours() {

                    function bindHoursWeek() {

                        function bindHoursClosedWeekTime() {

                            //////////////////////
                            var activeList = zXPath.selectNodes(tree, "//Children[Name/text()='指定营业时间']/Children[contains(Name/text(), '时间') and Selected/text()='true']");

                            $(".hours_table select", this.container).change(function() {
                                this.databind();
                                this.uibind();
                            }).each(function() {
                                this.databind = function() {
                                    var week_name = $(this).parent().parent().attr("tree_name");
                                    var time_name = $(this).attr("tree_name");
                                    var time_value = $(this).val();
                                    TreeFormManager.changeTreeNode(thisObj.datasource,
                                        "//Children[Name/text()='" + week_name + "']/Children[Name/text()='" + time_name + "']", true, time_value);
                                }
                                this.uibind = function() {
                                    //$(this).val();
                                    ;
                                }
                            });

                            jQuery.each(activeList, function() {
                                var week_name = TreeFormManager.getTreeNode(this, "Name");
                                $(".hours_table tr[tree_name='" + week_name + "'] select", this.container).each(function() {
                                    var time = TreeFormManager.getTreeNode(this, "Memo");
                                    if (time != "" && time != null) {
                                        this.val(time);
                                        this.uibind();
                                    }
                                });
                            });


                        }

                        bindHoursClosedWeekTime();

                        ///////////////////////////
                        var activeList = zXPath.selectNodes(tree, "//Children[Name/text()='指定营业时间']/Children[Selected/text()='true']");

                        $(".hours_table .closed", this.container).click(function() {
                            this.databind();
                            this.uibind();
                        }).each(function() {
                            this.databind = function() {
                                var week_name = $(this).parent().parent().attr("tree_name");
                                //debugger;
                                TreeFormManager.changeTreeNode(thisObj.datasource,
                                    "//Children[Name/text()='" + week_name + "']", !this.checked);
                            }
                            this.uibind = function() {
                                if (this.checked)
                                    $(this).parent().parent().find("select").attr('disabled', 'disabled');
                                else
                                    $(this).parent().parent().find("select").removeAttr('disabled');
                            }
                        });

                        jQuery.each(activeList, function() {
                            var week_name = TreeFormManager.getTreeNode(this, "Name");

                            $(".hours_table tr[tree_name='" + week_name + "'] .closed", this.container).each(function() {
                                this.checked = true;
                                this.uibind();
                            });
                        });

                    }

                    bindHoursWeek();


                    //////////////////
                    var activeName = TreeFormManager.getTreeNode(
                        zXPath.selectSingleNode(tree, "//*[Name/text()='营业时间']/Children[Selected/text()='true']"), "Name");

                    $(".hours_type", this.container).click(function() {
                        this.bind();

                        if ($(this).attr("tree_name") == "不指定营业时间") {
                            $('.am, .pm, .closed', thisObj.container).attr('disabled', 'disabled');
                        } else {
                            $('.closed', thisObj.container).removeAttr('disabled').each(function() { this.uibind(); });
                        }
                    }).each(function() {
                        this.bind = function() {
                            TreeFormManager.changeTreeNode(thisObj.datasource, "//*[Name/text()='营业时间']/Children", false);
                            TreeFormManager.changeTreeNode(thisObj.datasource, "//*[Name/text()='营业时间']/Children[Name/text()='" + $(this).attr("tree_name") + "']", this.checked);
                        }
                        if ($(this).attr("tree_name") == activeName)
                            this.click();
                    });
                }

                bindHours();
            },
            onDataNodeBind: function(path, node) {
            },
            onUpdated: function() { }
        },
        bindMappings: {}
    }));
    
</script>
</body>
</html>
